package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.entity.system.SysUserRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 分页查询角色信息方法
     * @param current 当前页
     * @param size 页大小
     * @param sysRoleDto 请求参数对象
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<SysRole> findByPage(Integer current, Integer size, SysRoleDto sysRoleDto) {
        //开启分页查询
        PageHelper.startPage(current,size);
        //根据查询条件返回list
        List<SysRole> list=sysRoleMapper.selectByPage(sysRoleDto);
        //根据查询信息创建分页对象
        PageInfo<SysRole> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    /**\
     * 新增角色方法
     * @param sysRole 角色对象
     */
    @Override
    public void addSysRole(SysRole sysRole) {
        sysRoleMapper.addSysRole(sysRole);
    }

    /**
     *  根据id查询角色信息方法
     * @param id 角色id
     * @return
     */
    @Override
    public SysRole getById(Long id) {
      SysRole sysRole=sysRoleMapper.getById(id);
        return sysRole;
    }

    /**
     * 根据id更新角色信息方法
     * @param sysRole 角色对象
     */
    @Override
    public void updateById(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    /**
     * 根据id删除角色
     * @param id 角色id
     */
    @Override
    public void deleteById(Long id) {
        sysRoleMapper.deleteById(id);
    }

    /**
     *  获取所有用户角色信息与根据用户id查询中间表的角色id
     * @param id 用户id
     * @return
     */
    @Override
    public Map<String, Object> toAssignPage(Long id) {
        List<SysRole> list=sysRoleMapper.getRoleList();
        Map<String,Object> map=new HashMap<>();
        map.put("allRoles",list);//所有角色名称
        List<Long> list2=sysUserRoleMapper.getRoleId(id);
        map.put("userRoleIds",list2);//当前用户角色id
        return map;
    }

    /**
     * 根据角色请求对象里的参数修改对应用户的角色id
     * @param assginRoleDto
     */
    @Override
    public void toAssginRole(AssginRoleDto assginRoleDto) {
        Long userId = assginRoleDto.getUserId();
        sysUserRoleMapper.deleteByUserId(userId);
        List<Long> roleIdList = assginRoleDto.getRoleIdList();
        if(roleIdList.size()>0){
        sysUserRoleMapper.doAssgin(assginRoleDto);
        }

    }
}
